<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * 总后台登陆
 */
class UserController extends Controller {
    
    function __construct() {
		parent::__construct();
		$admin_id = session('admin_id');
		if (empty($admin_id)) {
			$url = \Org\Net\Fun::url('admin/index/index');
			header("Location:" . $url);
			exit;
		}
	}
    
    public function index(){
       //echo "http://".$_SERVER['SERVER_NAME'].'/Public/touxiang/1.png';exit;
        $keyword = I("get.keyword");
        $where = array();
        $where[] = "(wx_id!='' AND image!='' AND name!='')";
        $whereString = "";
        if ($keyword) {
            $where[] = "(name='{$keyword}' OR wx_id='{$keyword}')";
        }
        $whereString = implode(" AND ", $where);
        $User = M('User'); // 实例化User对象
        $count = $User->where($whereString)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = $User->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count); // 赋值分页输出
        
        $this->display('index');
    }
    
    /**
     * 导出用户数据
     */
    public function DCuser(){
        $User = M('User'); // 实例化User对象
        $dataArr = $User->select();
        $headerData = array(
            '用户名字',
            '微信图片',
            '微信id',
            '分享数',
            '点击数',
            '添加时间',
        );
        $tableData = array();
        

        
        foreach ($dataArr as $key=>$item){
            $newData = array();
            $newData['name'] = $item['name'];
            $newData['image'] = $item['image'];
            $newData['wx_id'] = $item['wx_id'];
            
            $timeSto = $item['add_time'];
            $lastOne = substr($timeSto, -1, 1);
            
            
            
            //查询是否创建了宴会，创建了宴会则进证明分享过 begin
            $Banquet = M('Banquet'); 
            $whereArr = array(
                'wx_id'=>$item['wx_id']
            );
            $banquerData = $Banquet->where($whereArr)->find();
            if($banquerData['people']>1){
                $fxNumber = $lastOne+2;
            } else {
                $fxNumber = 0;
            }
            //查询是否创建了宴会，创建了宴会则进证明分享过 end
            
            
            $lastTwo = substr($timeSto, -2, 1);
            $lastTwo = $lastTwo?$lastTwo:2;
            
            $newData['aaaaa'] = $fxNumber;
            $newData['bbbbb'] = $lastTwo*2;
            
            $newData['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
            $tableData[] = $newData;
        }
        $file_name = "用户表".date('Y-m-d-His',time());
        \Org\Net\Fun::data_to_table($headerData, $tableData, $file_name);exit;
    }

    

    //兑换记录
    public function receive(){
        $keyword = I("get.keyword");
        $where = array();
        $whereString = implode(" AND ", $where);
        $Receive = M('Receive'); // 实例化User对象
        $count = $Receive->where($whereString)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        
        $list = $Receive
                ->field("user.name as wx_name,user.image as image,app_receive.exchange_code as code,app_receive.add_time as r_add_time")
                ->join("app_user AS user ON user.wx_id = app_receive.wx_id")
                ->where($where)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count); // 赋值分页输出
        
        $this->display('receive');
    }
    
    
    public function dcReceive(){
        $Receive = M('Receive'); // 实例化User对象
        $list = $Receive
                ->field("user.name as wx_name,user.image as image,app_receive.exchange_code as code,app_receive.add_time as r_add_time,exchange_code.address as address")
                ->join("app_user AS user ON user.wx_id = app_receive.wx_id")
                ->join("app_exchange_code AS exchange_code ON exchange_code.code = app_receive.exchange_code")
                ->where($where)
                ->select();
        
        $headerData = array(
            '使用的兑换码',
            '用户地址',
            '用户头像',
            '兑换用户微信昵称',
            '兑换日期',
        );
        $tableData = array();
        
        foreach ($list as $key=>$item){
            $newData = array();
            $newData['name'] = $item['code'];
            $newData['address'] = $item['address'];
            $newData['image'] = $item['image'];
            $newData['wx_id'] = $item['wx_name'];
            $newData['add_time'] = date('Y-m-d H:i:s',$item['r_add_time']);
            $tableData[] = $newData;
        }
        $file_name = "兑换表".date('Y-m-d-His',time());
        \Org\Net\Fun::data_to_table($headerData, $tableData, $file_name);exit;
        
        
        
    }




    public function banquet(){
        $keyword = I("get.keyword");
        $where = array();
        $whereString = implode(" AND ", $where);
        $Banquet = M('Banquet'); // 实例化User对象
        $count = $Banquet->where($whereString)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        
        
        $list = $Banquet->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();;
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count); // 赋值分页输出
        
        $this->display('banquet');
    }


    //导出排行榜
    public function dcBanquet(){

        $Banquet = M('Banquet'); // 实例化User对象
        $where = array(
            'is_calculation' => 1,
            'is_delete' => 0
        );
        $list = $Banquet->where($where)->order('people DESC')->select();
        
        $headerData = array(
            '名字',
            '电话',
            '地址',
            '宴会日期',
            '微信昵称',
            '微信头像',
            '参与人数',
            '参与日期',
        );
        $tableData = array();
        
        foreach ($list as $key=>$item){
            $newData = array();
            $newData['name'] = $item['name'];
            $newData['phone'] = $item['phone'];
            $newData['address'] = $item['address'];
            $newData['banquet_date'] = $item['banquet_date'];
            $newData['wx_name'] = $item['wx_name'];
            $newData['image'] = $item['image'];
            $newData['people'] = $item['people'];
            $newData['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
            $tableData[] = $newData;
        }
        $file_name = "排行榜－".date('Y-m-d-His',time());
        \Org\Net\Fun::data_to_table($headerData, $tableData, $file_name);exit;

    }


    //发起排行榜的用户
    public function dcBanquet2(){
        $Banquet = M('Banquet'); // 实例化User对象
        $where = array(
            'is_delete' => 0
        );
        $list = $Banquet->where($where)->order('people DESC')->select();
        
        $headerData = array(
            '名字',
            '电话',
            '地址',
            '宴会日期',
            '微信昵称',
            '微信头像',
            '参与人数',
            '参与日期',
        );
        $tableData = array();
        
        foreach ($list as $key=>$item){
            $newData = array();
            $newData['name'] = $item['name'];
            $newData['phone'] = $item['phone'];
            $newData['address'] = $item['address'];
            $newData['banquet_date'] = $item['banquet_date'];
            $newData['wx_name'] = $item['wx_name'];
            $newData['image'] = $item['image'];
            $newData['people'] = $item['people'];
            $newData['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
            $tableData[] = $newData;
        }
        $file_name = "发起宴会人列表－".date('Y-m-d-His',time());
        \Org\Net\Fun::data_to_table($headerData, $tableData, $file_name);exit;
    }

    
    
    public function insetData(){
        $Banquet = M('Banquet');
        
        $insetData = array();
        $insetData[1] = '张宏辉';
        $insetData[2] = '黄显运';
        $insetData[3] = '杨潇';
        $insetData[4] = '杨紫珩';
        $insetData[5] = '许诺';
        $insetData[6] = '杨建琦';
        $insetData[7] = '陈茹茹';
        $insetData[8] = '洪玉聪';
        $insetData[9] = '张高丽';
        $insetData[10] = '魏裳';
        
        $insetData[11] = '李梓凤';
        $insetData[12] = '张絮';
        $insetData[13] = '宋颖';
        $insetData[14] = '姚思颖';
        $insetData[15] = '列家伟';
        $insetData[16] = '刘世恩';
        $insetData[17] = '李友宇';
        $insetData[18] = '何南坊';
        $insetData[19] = '李雪云';
        $insetData[20] = '邱晓婷';
        
        $insetData[21] = '蔡宝逸';
        $insetData[22] = '陈怀谷';
        $insetData[23] = '黎欣欣';
        $insetData[24] = '袁晓凯';
        $insetData[25] = '李晓梅';
        $insetData[26] = '许晓舒';
        $insetData[27] = '叶素萍';
        $insetData[28] = '廖美婷';
        $insetData[29] = '黄彩虹';
        $insetData[30] = '陈晓霞';
        
        $insetData[31] = '蓝秋朔';
        $insetData[32] = '陈玉成';
        $insetData[33] = '俞叶';
        $insetData[34] = '陈志敏';
        $insetData[35] = '吴家宝';
        $insetData[36] = '赵开喜';
        $insetData[37] = '蔡秋玲';
        $insetData[38] = '宋克凡';
        $insetData[39] = '顾佳欣';
        $insetData[40] = '黄颖';
        
        $insetData[41] = '杨海霞';
        $insetData[42] = '李火茵';
        $insetData[43] = '陈捷';
        $insetData[44] = '林慧萍';
        $insetData[45] = '林旭文';
        $insetData[46] = '鞠晓明';
        $insetData[47] = '马智媛';
        $insetData[48] = '何厚铧';
        $insetData[49] = '杨小松';
        $insetData[50] = '方楠';
        
        $insetData[51] = '陈诗颖';
        $insetData[52] = '林晓燕';
        $insetData[53] = '王宝';
        $insetData[54] = '张子悦';
        $insetData[55] = '刘艳英';
        $insetData[56] = '陈本华';
        $insetData[57] = '吴广生';
        $insetData[58] = '赵金娥';
        $insetData[59] = '周毅';
        $insetData[60] = '刘凯文';
        
        $insetData[61] = '刘佳慧';
        $insetData[62] = '曹源';
        $insetData[63] = '郭丽叶';
        $insetData[64] = '陈思爽';
        $insetData[65] = '唐鸿雁';
        $insetData[66] = '郑凯旋';
        $insetData[67] = '黄宇杰';
        $insetData[68] = '谢秋银';
        $insetData[69] = '林欢';
        $insetData[70] = '温馨';
        
        $insetData[71] = '卢海薇';
        $insetData[72] = '列梓韦';
        $insetData[73] = '庞力靡';
        $insetData[74] = '陈雯';
        $insetData[75] = '黄柳风';
        $insetData[76] = '成家会';
        $insetData[77] = '郭雄恩';
        $insetData[78] = '卢锦文';
        $insetData[79] = '陈秋梅';
        $insetData[80] = '林志红';
        
        $insetData[81] = '姜泽通';
        $insetData[82] = '江泽桐';
        $insetData[83] = '林耿华';
        $insetData[84] = '曾泳醍';
        $insetData[85] = '莫一飞';
        $insetData[86] = '何安康';
        $insetData[87] = '曾参';
        $insetData[88] = '陈瑞生';
        $insetData[89] = '梅爱娇';
        $insetData[90] = '林慧';
        
        $insetData[91] = '盛南阳';
        $insetData[92] = '熊伟男';
        $insetData[93] = '柯月云';
        $insetData[94] = '秦汉';
        $insetData[95] = '吴登佳';
        $insetData[96] = '夏薇';
        $insetData[97] = '叶甜';
        $insetData[98] = '魏诺';
        $insetData[99] = '张悦迎';
        $insetData[100] = '叶满天';
        
        $insetData[101] = '王轩通';
        $insetData[102] = '龙勇祥';
        $insetData[103] = '李耀坤';
        $insetData[104] = '吴家明';
        $insetData[105] = '曾年娣';
        $insetData[106] = '赖桥带';
        $insetData[107] = '邱咏珊';
        $insetData[108] = '姚康贤';
        $insetData[109] = '单佳萍';
        $insetData[110] = '袁洁桢';
        
        $insetData[111] = '梁子君';
        $insetData[112] = '张文强';
        $insetData[113] = '陈俊言';
        $insetData[114] = '单梓君';
        $insetData[115] = '徐小琪';
        $insetData[116] = '刘芷君';
        $insetData[117] = '罗金伟';
        $insetData[118] = '陈龙凤';
        $insetData[119] = '陈秀霞';
        $insetData[120] = '邓嘉欣';
        
        $insetData[121] = '赖秀金';
        $insetData[122] = '吴文杰';
        $insetData[123] = '董咏诗';
        $insetData[124] = '陈志峰';
        $insetData[125] = '石润福';
        $insetData[126] = '赖志文';
        $insetData[127] = '邓泳君';
        $insetData[128] = '姚慧贤';
        $insetData[129] = '谢广龙';
        $insetData[130] = '梁思思';
        
        $insetData[131] = '刘雪芬';
        $insetData[132] = '蔡慧君';
        $insetData[133] = '钟洁怡';
        $insetData[134] = '黄紫娴';
        $insetData[135] = '刘嘉敏';
        $insetData[136] = '赖秀金';
        $insetData[137] = '何叔厚';
        $insetData[138] = '谢颖颖';
        $insetData[139] = '陈玉贤';
        
        $insetData[140] = '邓学文';
        $insetData[141] = '杨铭';
        $insetData[142] = '谢烤怡';
        $insetData[143] = '梁柳青';
        $insetData[144] = '蒋浩贤';
        $insetData[145] = '钟丽珍';
        $insetData[146] = '赖旭娟';
        $insetData[147] = '林荣鑫';
        $insetData[148] = '许铎涛';
        $insetData[149] = '苗震南';
        $insetData[150] = '温镇威';
        
        $insetData[151] = '刘玉珍';
        $insetData[152] = '叶静怡';
        $insetData[153] = '郭佳伟';
        $insetData[154] = '列梓杰';
        $insetData[155] = '陈炯宏';
        $insetData[156] = '汤晴';
        $insetData[157] = '黄洁珊';
        $insetData[158] = '郑旭超';
        $insetData[159] = '林银仲';
        $insetData[160] = '周彦';
        
        $insetData[161] = '林家琪';
        $insetData[162] = '卢晓杰';
        $insetData[163] = '吴玉媛';
        $insetData[164] = '刘品珍';
        $insetData[165] = '罗海玲';
        $insetData[166] = '邓思颖';
        $insetData[167] = '王艳芳';
        $insetData[168] = '曾敏仪';
        $insetData[169] = '付佳伟';
        $insetData[170] = '钟杏华';
        
        $insetData[171] = '饶少绸';
        $insetData[172] = '方海兴';
        $insetData[173] = '谭何为';
        $insetData[174] = '肖燕琼';
        $insetData[175] = '高婉嫔';
        $insetData[176] = '朱浩龙';
        $insetData[177] = '谢荣添';
        $insetData[178] = '马敏华';
        $insetData[179] = '徐雅';
        $insetData[180] = '刘国建';
        
        
        $inset = array();
        foreach($insetData as $key=>$item){
            $dc = array();
            $dc['name'] = $item;
            $dc['phone'] = 13800138000;
            $dc['address'] = "无地址";
            $dc['banquet_date'] = "10月1日";
            $dc['wx_id'] = "testwxid-".$key;
            $dc['wx_name'] = $item;
            $dc['image'] =  "http://".$_SERVER['SERVER_NAME'].'/Public/touxiang/'.$key.'.png';
            $dc['people'] =  rand(10,20);
            $dc['is_calculation'] =  1;
            $dc['is_first'] =  1;
            $dc['is_j'] =  1;
            $dc['add_time'] =  time();
            $inset[] = $dc;
        }
        $Banquet->addAll($inset);
        echo 'is ok';exit;
    }



    function updateJia(){
        $Banquet = M('Banquet');
        $banquetData = $Banquet->where("is_j=1 AND (id>=994 AND id<1054)")->select();
        foreach($banquetData as $key=>$item){
            $updateData = array();
            $updateData['people'] = rand(20,70);
            $updateData['is_delete'] = 0;
            $Banquet->where("id=".$item['id'])->save($updateData);
        }
        echo 'is ok';exit;

    }
    

}